$(document).ready(function() {
//datepicker
    if ($(".inputScheduleDateSubject").length > 0) {
        $(document).on('click', '.inputScheduleDateSubject', function(evt) {
            $(".inputScheduleDateSubject").datepicker({
                onSelect: function() {
                    $("div#sltDay").css("border", "red 1px solid");
                },
                dateFormat: 'dd/mm/yy',
            });
        });

    } else {
        $(".iptDate").datepicker({
            dateFormat: 'dd/mm/yy',
        });
    }



//popup create issue

    $(".popup-issue").fancybox({
        maxWidth: 800,
        maxHeight: 600,
        fitToView: false,
        width: '70%',
        height: '70%',
        autoSize: false,
        closeClick: false,
        openEffect: 'none',
        closeEffect: 'none'
    });

    //live search usser
    if ($("#live-search-user").length > 0) {
        $("#live-search-user").keyup(function() {
            var value = this.value;
            $("table#tbView tr").each(function(index) {
                if (index === 0)
                    return;
                var id = $(this).find("td:nth-child(3)").text();
                $(this).toggle(id.indexOf(value) !== -1);
            })
        });
    }

    if ($("#inputScheduleClass").length > 0) {
        $("#inputScheduleClass").change(function() {
            $("#inputScheduleDateSubject").focus();
            $.ajax({
                type: 'GET',
                url: "../Subject/getlist/" + $(this).val(),
                dataType: 'json',
                contentType: 'application/json',
                success: function(data) {
                    var slt = $("#inputScheduleSubject");
                    slt.empty();
                    if (data.length > 0) {
                        slt.append("<option value='-1'>Select Subject</option>");
                        //alert(data.length);

                        for (i = 0; i < data.length; i++) {
                            slt.append("<option value='" + data[i].subject_id + "'>" + data[i].subject_name + "</option>");
                        }
                    } else {
                        slt.append("<option>Empty</option>");
                    }

                }

            });
        });
    }
    if ($("#inputScheduleSubject").length > 0) {
        $("#inputScheduleSubject").change(function() {
//alert("Current value:" + $(this).val());
            if ($("#inputScheduleDateSubject").val().length <= 0) {
                alert("Please select beginning date");
                $("#inputScheduleDateSubject").focus();
                return false;
            }
            $.ajax({
                type: 'GET',
                url: "../Lession/getlist/" + $(this).val(),
                dataType: 'json',
                contentType: 'application/json',
                success: function(data) {
                    $(":input#inputNumberOfLession").val(data.length);
                    var rooms = $("#rooms");
                    rooms.empty();
                    var d_get = new Date($('#inputScheduleDateSubject').datepicker('getDate'));
                    if (data.length > 0) {
                        //alert(d_get.getDay() + 1);
                        if ($(".sltDayOfWeek").length > 0) {

                            var arrSltDayOfWeek = [];
                            $(".sltDayOfWeek:checked").each(function() {
                                arrSltDayOfWeek.push(parseInt(this.value));
                            });
                            //alert(arrSltDayOfWeek);
                            var totalLession = data.length;
                            var currentLession = 0;
                            if (arrSltDayOfWeek.length == 0) {
                                $("div#sltDay").css("border", "red 1px solid");
                                alert("Select day of week");
                                return false;
                            } else {
                                $("div#sltDay").removeAttr("style");
                            }
                            while (currentLession < totalLession) {
                                var dayOfWeek = d_get.getDay() + 1;
                                if (arrSltDayOfWeek.indexOf(dayOfWeek) == -1) {
                                    d_get.setDate(d_get.getDate() + 1);
                                } else {
                                    rooms.append("<div class='control-group'><label class='control-label' for='inputScheduleLession" + (currentLession + 1) + "'>Lession " + (currentLession + 1) + ": </label><div class='controls'><input type='text' id='inputScheduleLession" + (currentLession + 1) + "' class='inputScheduleDateSubject' name='inputScheduleLession" + (currentLession + 1) + "' value='" + ('0' + d_get.getDate()).slice(-2) + "/" + ('0' + (d_get.getMonth() + 1)).slice(-2) + "/" + d_get.getFullYear() + "'/><select id='lstLession" + (currentLession + 1) + "' name='lstLession" + (currentLession + 1) + "'></select></div></div>");
                                    d_get.setDate(d_get.getDate() + 1);
                                    currentLession++;
                                }
                            }
                            for (i = 1; i < totalLession + 1; i++) {
                                var sltListBox = $("#lstLession" + i);
                                for (j = 0; j < totalLession; j++) {
                                    var opSlt = "";
                                    if (i == (j + 1)) {
                                        opSlt = " selected='selected' ";
                                    }
                                    sltListBox.append("<option value='" + data[j].lession_id + "' " + opSlt + ">" + data[j].lession_name + "</option>");
                                }
                            }
                        } else {
                            var lstLession = $("#inputScheduleLession");
                            lstLession.empty();
                            lstLession.append("<option value='-1'>-------</option>")
                            for (var i = 0; i < data.length; i++) {
                                lstLession.append("<option value='" + data[i].lession_id + "' " + opSlt + ">" + data[i].lession_name + "</option>");
                            }
                        }

                    } else {
                        rooms.append("Empty lession");
                    }

                }
            });
        });
    }

//get schedule from date to date
    if ($("#btnGetListSchedule").length > 0) {
        $("#btnGetListSchedule").click(function() {
            if ($("#inputBeginDate").val().length <= 0) {
                $("#inputBeginDate").focus();
                return false;
            } else if ($("#inputEndDate").val().length <= 0) {
                $("#inputEndDate").focus();
                return false;
            } else {
                var fromDate = $("#inputBeginDate").val().replace(/\//g, "");
                var toDate = $("#inputEndDate").val().replace(/\//g, "");
                //alert("../schedule/getdatetodate/");
                $.ajax({
                    type: 'GET',
                    url: "../schedule/getdatetodate/" + fromDate + "to" + toDate,
                    data: 'json',
                    contentType: 'application/json',
                    success: function(data) {
                        var tblList = $("#tblSchedule tbody");
                        tblList.empty();
                        //alert(data.length);
                        if (data.length > 0) {
                            for (i = 0; i < data.length; i++) {
                                tblList.append("<tr><td><input name='id[]' type='checkbox' id='checkboxId[]' value='" + data[i].schedule_id + "'></td><td>" + data[i].classname + "</td><td>" + data[i].dates.split("-")[2] + "-" + data[i].dates.split("-")[1] + "-" + data[i].dates.split("-")[0] + "</td><td>" + data[i].lession_name + "</td><td>" + data[i].theory + "</td><td>" + data[i].lab + "</td><td><a class = 'btn' href = '../schedule/update/" + data[i].schedule_id + "' > <i class = 'icon-edit' > </i></a><a class = 'btn btn-danger' href = '#' onclick = \"ConfirmChoiceDelete('../schedule/delete/" + data[i].schedule_id + "');return false; \"><i class='icon-remove-sign'></i></a></td></tr>");
                            }
                        } else {
                            tblList.append("<tr><td colspan='7'>No lession</td></tr>");
                        }
                    }
                });
            }

        });
    }


});
// confirm when client delete an item
function ConfirmChoiceDelete(url)
{
    answer = confirm("Do you really want to delete it?")
    if (answer != 0)
    {
        location = url
    }
}

// confirm when client delete multi item
function ConfirmChoiceDeleteMulti()
{
    answer = confirm("Do you really want to delete it?")
    if (answer != 0)
    {
        return true;
    } else {
        return false;
    }
}
